load("@fbcode//buck2:buck_rust_binary.bzl", "buck_rust_binary")
load("@fbcode_macros//build_defs:rust_library.bzl", "rust_library")
load(
    "@fbsource//tools/build_defs:audit_dependencies_test.bzl",
    "audit_dependencies_test",
)
load("@fbsource//tools/build_defs:glob_defs.bzl", "glob")

oncall("build_infra")

rust_library(
    name = "buck2",
    srcs = glob(["src/**/*.rs"]),
    os_deps = [
        (
            "linux",
            [
                "fbsource//third-party/rust:nix",
            ],
        ),
        (
            "macos",
            [
                "fbsource//third-party/rust:nix",
            ],
        ),
        (
            "windows",
            [
                "fbsource//third-party/rust:winapi",
            ],
        ),
    ],
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:async-trait",
        "fbsource//third-party/rust:backtrace",
        "fbsource//third-party/rust:clap-3",
        "fbsource//third-party/rust:dirs",
        "fbsource//third-party/rust:futures",
        "fbsource//third-party/rust:libc",
        "fbsource//third-party/rust:rand",
        "fbsource//third-party/rust:serde",
        "fbsource//third-party/rust:serde_json",
        "fbsource//third-party/rust:termimad",
        "fbsource//third-party/rust:thiserror",
        "fbsource//third-party/rust:tokio",
        "fbsource//third-party/rust:tokio-stream",
        "fbsource//third-party/rust:tracing",
        "//buck2/allocative/allocative:allocative",
        "//buck2/app/buck2_audit:buck2_audit",
        "//buck2/app/buck2_audit_server:buck2_audit_server",
        "//buck2/app/buck2_cli_proto:buck2_cli_proto",
        "//buck2/app/buck2_client:buck2_client",
        "//buck2/app/buck2_client_ctx:buck2_client_ctx",
        "//buck2/app/buck2_common:buck2_common",
        "//buck2/app/buck2_core:buck2_core",
        "//buck2/app/buck2_data:buck2_data",
        "//buck2/app/buck2_event_observer:buck2_event_observer",
        "//buck2/app/buck2_events:buck2_events",
        "//buck2/app/buck2_forkserver:buck2_forkserver",
        "//buck2/app/buck2_query:buck2_query",
        "//buck2/app/buck2_server:buck2_server",
        "//buck2/app/buck2_server_ctx:buck2_server_ctx",
        "//buck2/app/buck2_starlark:buck2_starlark",
        "//buck2/app/buck2_test_runner:buck2_test_runner",
        "//buck2/app/buck2_util:buck2_util",
        "//buck2/app/buck2_wrapper_common:buck2_wrapper_common",
        "//buck2/dice/dice:dice",
        "//buck2/gazebo/dupe:dupe",
        "//common/rust/gflags:gflags",
        "//common/rust/shed/fbinit:fbinit",
    ],
)

buck_rust_binary(
    name = "buck2-bin",
    srcs = ["bin/buck2.rs"],
    crate = "buck2",
    crate_root = "bin/buck2.rs",
    env = {
        # This is only set for release builds.
        "BUCK2_RELEASE_TIMESTAMP": read_config("buck", "release_timestamp", ""),
        # This config value is set by the upload script, and controls the output of `--version`. We
        # would like to use build info for this, but it is unfortunately not reliably available, see
        # T140147872.
        "BUCK2_SET_EXPLICIT_VERSION": read_config("buck", "set_explicit_version", ""),
        # This config value is set by the upload script, and controls the output of `--internal-version` on windows
        "BUCK2_WIN_INTERNAL_VERSION": read_config("buck", "win_internal_version", ""),
    },
    unittests = False,
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:tracing",
        "//buck2/app/buck2:buck2",
        "//buck2/app/buck2_action_impl:buck2_action_impl",
        "//buck2/app/buck2_analysis:buck2_analysis",
        "//buck2/app/buck2_anon_target:buck2_anon_target",
        "//buck2/app/buck2_audit_server:buck2_audit_server",
        "//buck2/app/buck2_build_api:buck2_build_api",
        "//buck2/app/buck2_build_info:buck2_build_info",
        "//buck2/app/buck2_build_signals_impl:buck2_build_signals_impl",
        "//buck2/app/buck2_bxl:buck2_bxl",
        "//buck2/app/buck2_client_ctx:buck2_client_ctx",
        "//buck2/app/buck2_configured:buck2_configured",
        "//buck2/app/buck2_core:buck2_core",
        "//buck2/app/buck2_events:buck2_events",
        "//buck2/app/buck2_interpreter_for_build:buck2_interpreter_for_build",
        "//buck2/app/buck2_query_impls:buck2_query_impls",
        "//buck2/app/buck2_server_commands:buck2_server_commands",
        "//buck2/app/buck2_test:buck2_test",
        "//buck2/app/buck2_transition:buck2_transition",
        "//buck2/app/buck2_wrapper_common:buck2_wrapper_common",
        "//buck2/gazebo/dupe:dupe",
        "//common/rust/folly/logging:logging",
        "//common/rust/gflags:gflags",
        "//common/rust/shed/fbinit:fbinit",
    ],
)

# We use //third-party/blake3/blake3-rust to avoid ODR violations instead.
audit_dependencies_test(
    name = "ban_third_party_rust_blake3",
    blocklist_patterns = [
        "fbsource//third-party/rust:blake3.*",
    ],
    # audit_dependencies_test doesn't work on Windows as the macro relies on bash
    compatible_with = [
        "ovr_config//os:linux",
        "ovr_config//os:macos",
    ],
    contacts = ["oncall+build_infra@xmail.facebook.com"],
    rule = ":buck2",
)
